/**
 * 
 */
package vue.popup;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import vue.session.IGCNFUse;
import controller.ISessionManager;

/**
 * A pop up menu for {@link IGCNFUse}.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class JPopupCNFUse extends JPopupMenu implements IJPopupMenu, ActionListener {


	private static final long	serialVersionUID	= 1L;

	public static final String	ITEM_PREPROC		= "Execute preprocessor";
	public static final String	ITEM_SOLVER			= "Execute solver";
	public static final String	ITEM_STAT			= "Check stat";


	protected JMenuItem			executeSolverItem;
	protected JMenuItem			executePreprocItem;
	protected JMenuItem			checkStatItem;

	private ISessionManager		manager;
	private IGCNFUse			cnfuse;

	/**
	 * Create the pop {@link JPopupCNFUse}.
	 * 
	 * @param cnfuse
	 *            the {@link IGCNFUse} associate
	 * @param manager
	 *            the session manager
	 */
	public JPopupCNFUse(IGCNFUse cnfuse, ISessionManager manager) {
		this.cnfuse = cnfuse;
		this.manager = manager;

		createMenu();

		refresh();
	}


	/**
	 * Create the pop up menu for the CNF.
	 */
	protected void createMenu() {
		// preproc
		executePreprocItem = new JMenuItem(ITEM_PREPROC);
		executePreprocItem.addActionListener(this);

		add(executePreprocItem);

		// solver
		executeSolverItem = new JMenuItem(ITEM_SOLVER);
		executeSolverItem.addActionListener(this);

		add(executeSolverItem);

		addSeparator();

		// stat
		checkStatItem = new JMenuItem(ITEM_STAT);
		checkStatItem.addActionListener(this);

		add(checkStatItem);
	}


	@Override
	public void refresh() {
		// TODO Auto-generated method stub

	}

	/**
	 * Open a dialog to execute a solver on the cnf.
	 */
	public void executeSolverMenu() {
		IGSolverDialog dialog = new IGSolverDialog(manager, cnfuse.getCnfuse(), null);
		dialog.setVisible(true);
	}


	/**
	 * Open a dialog to execute a preprocessor on the cnf.
	 */
	public void executePreprocMenu() {
		IGPreprocessorDialog dialog = new IGPreprocessorDialog(manager, cnfuse.getCnfuse(), null);
		dialog.setVisible(true);
	}

	/**
	 * Ask to check the stat of the cnf.
	 */
	public void checkStat() {
		manager.checkCNFStat(cnfuse.getCnfuse());
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		if (e.getSource() == executePreprocItem)
		{// execute preproc
			executePreprocMenu();
			return;
		}
		if (e.getSource() == executeSolverItem)
		{// execute solver
			executeSolverMenu();
			return;
		}
		if (e.getSource() == checkStatItem)
		{// check stat
			checkStat();
			return;
		}
	}

}
